/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__req_data_analysis.c                             */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/31 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <memcached/memcached_core.h>
#include "frsutl.h"
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frreport_comm.h"
#include "../../../src/module/memcached/config.h"
#include "frbyte_buffer.h"

typedef struct __st_L7session_memcached_private
{
    unsigned long ul_seq;                             /*!< 序列号，1次req和response时，使用同一个序列号 */
    L4_transaction st_l4_trans;                       /*!< 数据 */
    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    Frlist_header *p_lst_comm;
    Frlist_header *p_lst_req;
    Frbyte_Packet *p_req_packet;                      /*!< 存请求bytebuff数据 */
    Frbyte_Packet *p_resp_packet;                     /*!< 存相应bytebuff数据 */
    int i_sign_get;                                   /*!< 标志位，1:为取回命令 */
    int i_sign_gets;
    int i_sign_stats;                                 /*!< 标志位，1:为状态命令 */
}L7session_memcached_private;

extern L7session_memcached_private* __memcached_private_create(void);
extern int __req_data_analysis(Conntrack *p_st_conn
                                , Apm_packet *p_st_data_in
                                , Frhash_header *p_st_hash_report
                                , L7_session *p_st_session);
extern int memcached_session_destroy(void *p_private_session);
extern void list_report_node_free(Frlist_node *p_st_node);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    Frhash_header *p_st_hash_report = NULL;
    L7_session *p_st_session = NULL;

    i_ret = __req_data_analysis(p_st_conn, p_st_data_in, p_st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_PTR_EQUAL(p_st_session, NULL);

    return;
}

static void case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    frhash2_create(&st_hash_report, 2);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("add hello 4 0 4      \r\n1234\r\n");
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 0;
    p_st_data_in->st_timestamp.tv_sec = 11;
    p_st_data_in->st_timestamp.tv_usec = 22;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __memcached_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_get = 0;
    ((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_stats = 0;

    i_ret = __req_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_get, 0);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_stats, 0);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.l_request_bytes, 0);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec, 11);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec, 22);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec, 0);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec, 0);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    memcached_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

static void case3(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    frhash2_create(&st_hash_report, 2);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("add hello 4 0 4      \r\n1234\r\n");
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 29;
    p_st_data_in->st_timestamp.tv_sec = 11;
    p_st_data_in->st_timestamp.tv_usec = 22;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __memcached_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_get = 0;
    ((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_stats = 0;

    i_ret = __req_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC));
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_get, 0);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_stats, 0);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.l_request_bytes, 29);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec, 11);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec, 22);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec, 11);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec, 22);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    memcached_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

static void case4(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    frhash2_create(&st_hash_report, 2);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("get hello\r\n");
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 11;
    p_st_data_in->st_timestamp.tv_sec = 11;
    p_st_data_in->st_timestamp.tv_usec = 22;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __memcached_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_get = 0;
    ((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_stats = 0;

    i_ret = __req_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC));
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_get, 1);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_stats, 0);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.l_request_bytes, 11);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec, 11);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec, 22);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec, 11);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec, 22);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    memcached_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

static void case5(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    frhash2_create(&st_hash_report, 2);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("stats\r\n");
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 7;
    p_st_data_in->st_timestamp.tv_sec = 11;
    p_st_data_in->st_timestamp.tv_usec = 22;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __memcached_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_get = 0;
    ((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_stats = 1;

    i_ret = __req_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC));
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_get, 0);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->i_sign_stats, 1);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.l_request_bytes, 7);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec, 11);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec, 22);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec, 11);
    CU_ASSERT_EQUAL(((L7session_memcached_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec, 22);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    memcached_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

int add_suite__req_data_analysis(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __req_data_analysis()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}